import axios from "axios";
import urls from "@/http/urls";
import store from "@/store/index";
import router from "@/router/index";
// import { config } from "vue/types/umd";

const instance = axios.create({
  baseURL: urls.baseUrl,
  timeout: 5000,
});

//请求拦截器主要是判断 store 是否含有 token ，如果有的话就加在请求头上
instance.interceptors.request.use((config) => {
  if (store.state.token) {
    config.headers.Authorization = store.state.token;
  }
  return config;
});

//响应拦截器，主要用于过滤错误的响应，这里只设置了对未登录状态码的拦截
instance.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    //默认除了2XX之外的都是错误的，就会走这里
    if (error.response) {
      switch (error.response.status) {
        case 401:
          router.replace({
            path: "login",
            // 将跳转的路由path作为参数，登录成功后跳转到该路由
            query: { redirect: router.currentRoute.fullPath },
          });
      }
    }
    return Promise.reject(error.response);
  }
);

export default {
  getArticles(page, limit) {
    return (
      instance
        .get(urls.articles, {
          params: {
            page: page,
            limit: limit,
          },
        })
        // 这里的 res => res.data 是为了防止axios中的data属性和返回值中的data属性混淆
        .then((res) => res.data)
    );
  },
  getArticleByID(id) {
    return instance.get(urls.article + "/" + id).then((res) => res.data);
  },

  getAllTags() {
    return instance.get(urls.tags).then((res) => res.data);
  },
  getArticlesByTag(tag, page, limit) {
    return instance
      .get(urls.tag + "/" + tag, {
        params: {
          page: page,
          limit: limit,
        },
      })
      .then((res) => res.data);
  },

  getAllCategories() {
    return instance.get(urls.categories).then((res) => res.data);
  },
  getArticlesByCategory(category, page, limit) {
    return instance
      .get(urls.category + "/" + category, {
        params: {
          page: page,
          limit: limit,
        },
      })
      .then((res) => res.data);
  },

  getBlogInfo() {
    return instance.get(urls.info).then((res) => res.data);
  },

  getTimeline() {
    return instance.get(urls.timeline).then((res) => res.data);
  },
};
